package com.cg.asm.body.service.roles.impl;

import com.cg.asm.body.dao.roles.RoleFunMapper;
import com.cg.asm.body.dao.roles.RolesMapper;
import com.cg.asm.body.dao.user.UserGroupMapper;
import com.cg.asm.body.domain.dto.roles.RolesDTO;
import com.cg.asm.body.domain.vo.roles.RolesChoiceVO;
import com.cg.asm.body.domain.vo.roles.RolesPagedVO;
import com.cg.asm.body.service.roles.IRolesInfoService;
import com.cg.asm.body.util.AsmBodyCommonUtil;
import com.cg.asm.body.util.constants.DicEnum;
import com.cg.asm.client.domain.user.UserInfo;
import com.cg.asm.client.exception.AsmException;
import com.cg.asm.client.exception.MybatisExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RolesInfoService implements IRolesInfoService {

    private final RolesMapper rolesMapper;
    private final RoleFunMapper roleFunMapper;
    private final UserGroupMapper userGroupMapper;

    public RolesInfoService(RolesMapper rolesMapper, RoleFunMapper roleFunMapper, UserGroupMapper userGroupMapper) {
        this.rolesMapper = rolesMapper;
        this.roleFunMapper = roleFunMapper;
        this.userGroupMapper = userGroupMapper;
    }


    @Override
    public PageInfo<RolesPagedVO> selectByRolesInfo(MybatisExample mybatisExample) {
        Map<String, String> param = new HashMap<>();
        String userId = AsmBodyCommonUtil.getCurrentUserLoginVO().getUserId();
        if (!"super".equals(userId)) {
            //如果不是超级管理员（super），则只查询自己创建的角色
            param.put("creatorUserId", userId);
        }
        mybatisExample.setParameter(AsmBodyCommonUtil.mybatisExampleMergeParam(mybatisExample, param));
        PageHelper.startPage(mybatisExample.getOffset(), mybatisExample.getLimit());
        List<RolesPagedVO> roles = rolesMapper.selectByRolesInfo(mybatisExample);
        return new PageInfo<>(roles);
    }

    @Override
    public boolean roleIsUnique(String roleId) {
        if (StringUtil.isNotEmpty(roleId)) {
            String valiRoleId = "ROLE_" + roleId.toUpperCase();
            String resultValue = rolesMapper.roleIsUnique(valiRoleId);
            if (StringUtil.isNotEmpty(resultValue)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void saveNewRole(RolesDTO rolesDTO) {
        if (null != rolesDTO && StringUtil.isNotEmpty(rolesDTO.getRoleId())) {
            String saveRoleId = "ROLE_" + rolesDTO.getRoleId().toUpperCase();
            rolesDTO.setRoleId(saveRoleId);
            rolesDTO.setCreatorDate(new Date());
            rolesDTO.setCreatorUserId(AsmBodyCommonUtil.getCurrentUserLoginVO().getUserId());
            rolesDTO.setIsBuilt("no");
            rolesMapper.saveNewRole(rolesDTO);
        } else {
            throw new AsmException("保存角色信息失败，未获取角色信息，或角色ID为空！");
        }
    }

    @Override
    public void delRole(String[] roleIds) {
        if (null != roleIds && roleIds.length != 0) {
            rolesMapper.delRole(roleIds);
            rolesMapper.delUserRole(roleIds);
            userGroupMapper.delUserGroupRoleByRoleId(roleIds);
            for (String roleId : roleIds) {
                roleFunMapper.delRoleFun(roleId);
                roleFunMapper.delRoleFunPoints(roleId);
            }
        } else {
            throw new AsmException("删除失败，角色ID不能为空!");
        }
    }

    @Override
    public void saveEditRole(RolesDTO rolesDTO) {
        if (null != rolesDTO && StringUtil.isNotEmpty(rolesDTO.getRoleId())) {
            rolesMapper.saveEditRole(rolesDTO);
        } else {
            throw new AsmException("更新角色信息失败，角色Id不能为空！");
        }
    }

    @Override
    public List<RolesChoiceVO> roleByManager() {
        List<RolesChoiceVO> rolesChoiceVOS = new ArrayList<>();
        UserInfo userLoginVO = AsmBodyCommonUtil.getCurrentUserLoginVO();
        if (null != userLoginVO) {
            String userId = userLoginVO.getUserId();
            String manager = DicEnum.ROLE_TYPE_MANAGER.getKey();
            rolesChoiceVOS = rolesMapper.getRoleInfo(manager, userId);
        }
        return rolesChoiceVOS;
    }

    @Override
    public List<RolesChoiceVO> roleByExecute() {
        List<RolesChoiceVO> rolesChoiceVOS = new ArrayList<>();
        UserInfo userLoginVO = AsmBodyCommonUtil.getCurrentUserLoginVO();
        if (null != userLoginVO) {
            String userId = userLoginVO.getUserId();
            String execute = DicEnum.ROLE_TYPE_EXECUTE.getKey();
            rolesChoiceVOS = rolesMapper.getRoleInfo(execute, userId);
        }
        return rolesChoiceVOS;
    }
}
